/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figure A.1 (b)

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
set seed 12345
local curr_date = c(current_date)
log using "${logs}/FigA1b`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${EPH}/eph_cont_2009_2018.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/

*Run program for quantiles
run "${utilities}/quantiles.do"

*Quantiles for temp workers (restrictive)
cuantiles pp3e_tot [fw=pondera] if industria_ocup_prin_caes == 78 & pp07c == 1 & pp3e_tot <80 & cat_ocup == 3 & estado == 1 & ano >2011, orden_aux(ano) n(10) g(hours_temp_q)
bys hours_temp_q : egen mean_hours_temp_q = mean(pp3e_tot)
bys hours_temp_q : gen aux_temp = 1 if _n == 1

*Quantiles for temp workers (non-restrictive)
cuantiles pp3e_tot [fw=pondera] if industria_ocup_prin_caes == 78 & pp3e_tot <80 & cat_ocup == 3 & estado == 1 & ano >2011, orden_aux(ano) n(10) g(hours_temp_nor_q)
bys hours_temp_nor_q : egen mean_hours_temp_nor_q = mean(pp3e_tot)
bys hours_temp_nor_q : gen aux_temp_nor = 1 if _n == 1

*Quantiles for reg workers
cuantiles pp3e_tot [fw=pondera] if industria_ocup_prin_caes != 78 & pp3e_tot <80 & cat_ocup == 3 & estado ==1 & ano>2011, orden_aux(ano) n(10) g(hours_reg_q)
bys hours_reg_q : egen mean_hours_reg_q = mean(pp3e_tot)
bys hours_reg_q : gen aux_reg = 1 if _n == 1

*Prepare cumulative stats
cumul mean_hours_temp_q [fw=pondera] if aux_temp == 1 & mean_hours_temp_q !=. & hours_temp_q != ., gen(chours_temp)
cumul mean_hours_temp_nor_q [fw=pondera] if aux_temp_nor == 1 & mean_hours_temp_nor_q !=. & hours_temp_nor_q != ., gen(chours_temp_nor)
cumul mean_hours_reg_q [fw=pondera] if aux_reg == 1 & mean_hours_reg_q !=. & hours_reg_q != ., gen(chours_reg)

*Plot Figure
twoway (line chours_temp mean_hours_temp_q if mean_hours_temp_q != . & chours_temp != . , sort lwidth(0.5) lpattern(dot)) (line chours_temp_nor mean_hours_temp_nor_q if mean_hours_temp_nor_q != . & chours_temp_nor != . , sort lwidth(0.5) lpattern(dash)) (line chours_reg mean_hours_reg_q if mean_hours_reg_q != . & chours_reg != . , sort lwidth(0.5) lpattern(solid)),  /*
*/ xtitle(" Weekly Hours", size(medlarge)) ytitle("Cumulative density",  size(medlarge)) subtitle(,lcolor(white) fcolor(white)) graphregion(color(white)) plotregion(color(white)) legend(cols(1)order(1 "(Restrictive) ==> (employer industry code + fixed term work)" 2 "(Non-Restrictive) ==> (employer industry code)" 3 "Regular Workers" )position(6)region(color(white))) xsize(16) ysize(9)
graph export "${Results}/CumulativeDist_reg_temp_hours.pdf", replace

*Summary Statistics

*Temp Workers (restrictive)
sum pp3e_tot [fw=pondera] if industria_ocup_prin_caes == 78 & pp07c == 1 & pp3e_tot <80 & cat_ocup == 3 & estado == 1 & ano >2011

*Ttemp Workers (non-restrictive)
sum pp3e_tot [fw=pondera] if industria_ocup_prin_caes == 78 & pp3e_tot <80 & cat_ocup == 3 & estado == 1 & ano >2011

*Regular Workers
sum pp3e_tot [fw=pondera] if industria_ocup_prin_caes != 78 & pp3e_tot <80 & cat_ocup == 3 & estado ==1 & ano>2011
